package keeto.com.vn.bms.config;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

import keeto.com.vn.bms.model.HospitalRecordInputDataModel;
import keeto.com.vn.bms.model.InputDataModel;
import keeto.com.vn.bms.model.User.EditUserModel;
import keeto.com.vn.bms.network.Command;
import keeto.com.vn.bms.network.IBMSProtocol;
import keeto.com.vn.bms.network.IRunable;
import keeto.com.vn.bms.network.JSONProtocol;
import keeto.com.vn.bms.network.task.TaskAddTechnical;
import keeto.com.vn.bms.network.task.TaskCreateSchedule;
import keeto.com.vn.bms.network.task.TaskDeleteMsg;
import keeto.com.vn.bms.network.task.TaskDeleteTechnical;
import keeto.com.vn.bms.network.task.TaskEditUserInfo;
import keeto.com.vn.bms.network.task.TaskGetComDoctor;
import keeto.com.vn.bms.network.task.TaskGetComment;
import keeto.com.vn.bms.network.task.TaskGetDetailMesseger;
import keeto.com.vn.bms.network.task.TaskGetDetailReportByType;
import keeto.com.vn.bms.network.task.TaskGetDetailReportFile;
import keeto.com.vn.bms.network.task.TaskGetDetailSergery;
import keeto.com.vn.bms.network.task.TaskGetHopital;
import keeto.com.vn.bms.network.task.TaskGetListComHospital;
import keeto.com.vn.bms.network.task.TaskGetListCompany;
import keeto.com.vn.bms.network.task.TaskGetListCourses;
import keeto.com.vn.bms.network.task.TaskGetListHints;
import keeto.com.vn.bms.network.task.TaskGetListMedicalRecord;
import keeto.com.vn.bms.network.task.TaskGetListMedicalRecordHospital;
import keeto.com.vn.bms.network.task.TaskGetListMesseger;
import keeto.com.vn.bms.network.task.TaskGetListNotification;
import keeto.com.vn.bms.network.task.TaskGetListProduct;
import keeto.com.vn.bms.network.task.TaskGetListPromotions;
import keeto.com.vn.bms.network.task.TaskGetListSeminar;
import keeto.com.vn.bms.network.task.TaskGetListSurgery;
import keeto.com.vn.bms.network.task.TaskGetMessegerTwoGroups;
import keeto.com.vn.bms.network.task.TaskGetProductBMS;
import keeto.com.vn.bms.network.task.TaskGetSchedule;
import keeto.com.vn.bms.network.task.TaskGetSupport;
import keeto.com.vn.bms.network.task.TaskGetTimeNotification;
import keeto.com.vn.bms.network.task.TaskGetTypeUser;
import keeto.com.vn.bms.network.task.TaskGetUserInfo;
import keeto.com.vn.bms.network.task.TaskLogout;
import keeto.com.vn.bms.network.task.TaskPoseNotParticipate;
import keeto.com.vn.bms.network.task.TaskSendComment;
import keeto.com.vn.bms.network.task.TaskSendDeleteMessager;
import keeto.com.vn.bms.network.task.TaskSendDeleteNotify;
import keeto.com.vn.bms.network.task.TaskSendMesseger;
import keeto.com.vn.bms.network.task.TaskSendMsgtoGroups;
import keeto.com.vn.bms.network.task.TaskSendToAdmin;
import keeto.com.vn.bms.network.task.TaskSendUpdateComDoctor;
import keeto.com.vn.bms.network.task.TaskSendUpdateHospitalRecord;
import keeto.com.vn.bms.network.task.TaskSentStatusSchedule;
import keeto.com.vn.bms.network.task.TaskSetReadMessage;
import keeto.com.vn.bms.network.task.TaskSetReadNotify;
import keeto.com.vn.bms.network.task.TaskUpdateAndroidSchedule;
import keeto.com.vn.bms.network.task.TaskUserLogon;
import keeto.com.vn.bms.utils.BMSListener;
import keeto.com.vn.bms.utils.BMSSharedPreferences;
import android.content.Context;
import android.os.Process;

public class BMSCore implements IBMSCore, IBMSConfig, Runnable {
	public static final int PRIORITY_BACKGROUND = 1;
	public static final int PRIORITY_LOW = 2;
	public static final int PRIORITY_MEDIUM = 3;
	public static final int PRIORITY_HIGH = 4;
	public static final int PRIORITY_TOP = 5;

	private static BMSCore instance = null;
	private Context mContext;
	private IBMSProtocol protocol;
	private static Set<BMSListener> mListener = new CopyOnWriteArraySet<BMSListener>();
	private String authKey = UNINITIALIZED_STRING;
	private BMSSharedPreferences mPreferences;
	private static boolean networkFailed = false;
	private BlockingQueue<Command> mCommands = new PriorityBlockingQueue<Command>();
	private Thread mThread;
	boolean coreFlag = false;
	public static Boolean httpPollingBusy = false;
	private boolean connected = true;
	private byte[] connectionLock = new byte[0];
	public int sleepTime = 10 * 1000;

	public BMSCore(Context context) {
		this.mContext = context;
		this.protocol = new JSONProtocol(context, this);
		this.mPreferences = new BMSSharedPreferences(context);
		mThread = new Thread(this);
		mThread.setName("BMSCORE");
		mThread.start();
	}

	public synchronized static BMSCore getInstance(Context context) {
		if (instance == null) {
			instance = new BMSCore(context);
		}
		return instance;
	}

	public void addListener(BMSListener listener) {
		if (listener != null) {
			mListener.add(listener);
		}
	}

	public void removeListener(BMSListener listener) {
		mListener.remove(listener);
	}

	@Override
	public IBMSProtocol getProtocol() {
		// TODO Auto-generated method stub
		return protocol;
	}

	@Override
	public Context getContext() {
		// TODO Auto-generated method stub
		return mContext;
	}

	@Override
	public String getAuthKey() {
		// TODO Auto-generated method stub
		return (authKey.equals(UNINITIALIZED_STRING)) ? mPreferences
				.getUserAuthKey(mContext) : authKey;
	}

	@Override
	public Set<BMSListener> copyAndReconstructListeners(BMSListener listener) {
		// TODO Auto-generated method stub
		if (listener == null) {
			return mListener;
		}
		Set<BMSListener> listeners = new HashSet<BMSListener>(mListener);
		listeners.add(listener);
		return listeners;
	}

	@Override
	public boolean isNetworkFail() {
		// TODO Auto-generated method stub
		return networkFailed;
	}

	private void restartCore() {
		coreFlag = false;
		mThread = new Thread(this);
		mThread.start();
	}

	public static AtomicInteger sequencing = new AtomicInteger(0);

	public void put(BMSListener listener, String action, int priority,
			IRunable runable, boolean sendByUI) {

		putCommand(mCommands, action, listener, runable, priority, sendByUI,
				Command.DEFAULT_RETRY_TIMES);

	}

	private void putCommand(BlockingQueue<Command> queue, String action,
			BMSListener listener, IRunable runnable, int priority,
			boolean sendByUI, int retryTimes) {
		int retries = 10;
		Exception e = null;
		while (retries-- > 0) {
			try {

				Command command = new Command(runnable, listener, action,
						priority, sendByUI);
				queue.put(command);
				return;
			} catch (InterruptedException ie) {
				try {
					Thread.sleep(IBMSConfig.PUT_COMMAND_RETRY_INTERVAL);
				} catch (InterruptedException ne) {
				}
				e = ie;
			}
		}
		throw new Error(e);
	}

	public static void setHttpPollingBusy(Boolean value) {
		httpPollingBusy = value;
	}

	public static Boolean getHttpPollingBusy() {
		return httpPollingBusy;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
		coreFlag = true;
		while (coreFlag) {
			String commandDescription = null;
			try {
				final Command command = mCommands.take();
				if (!connected) {
					if (command != null) {
						if (command.sendByUI) {
							command.runable.timeoutCallback();
							continue;
						} else {
							// if (command.action.equals(TaskHeartbeat.class
							// .getName())) {
							// continue;
							// }
							mCommands.add(command);
						}
					}
					synchronized (connectionLock) {
						connectionLock.wait(NETWORK_FAIL_WAIT);
						continue;
					}
				}

				if (command != null) {
					commandDescription = command.action;
					try {
						command.runable.run();
						resetSleepTime();
					} catch (Exception e) {
						if (command.sendByUI) {
							command.runable.timeoutCallback();
						} else {
							if (command.retryTime > 0) {
								command.retryTime--;
								new Thread() {
									@Override
									public void run() {
										try {
											sleep(increaseSleepTime());
											mCommands.put(command);
										} catch (InterruptedException e) {
										}
									}

								}.start();
							}
						}
						e.printStackTrace();
					}
					// callback execution complete
					for (BMSListener l : copyAndReconstructListeners(command.listener)) {
						l.commandExecutionComplete(mCommands.size() > 0);
					}
				}
			}
			// prevent the Core thread from crash.
			catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void resetSleepTime() {
		sleepTime = 10 * 1000;
	}

	private int increaseSleepTime() {
		if (sleepTime == 10 * 1000) {
			// 30 seconds
			sleepTime = 30 * 1000;
		} else if (sleepTime == 30 * 1000) {
			// 3 minutes
			sleepTime = 3 * 60 * 1000;
		} else if (sleepTime == 3 * 60 * 1000) {
			// 5 minutes
			sleepTime = 5 * 60 * 1000;
		}
		return sleepTime;
	}

	@Override
	public void stopThreads() {
		// TODO Auto-generated method stub
		mCommands.clear();
		restartCore();
	}

	@Override
	public void stopCoreAndDestroy() {
		this.stopThreads();
		mThread.interrupt();
		mThread = null;
	}

	/*
	 * Task Connection
	 */

	@Override
	public int taskLogon(String username, String password, BMSListener listener) {
		// TODO Auto-generated method stub
		TaskUserLogon taskUserLogon = new TaskUserLogon(listener, instance,
				username, password);
		put(listener, taskUserLogon.getClass().getName(), PRIORITY_MEDIUM,
				taskUserLogon, true);

		return 0;
	}

	@Override
	public int taskLogout(String Authkey, BMSListener bmsListener) {
		// TODO Auto-generated method stub
		TaskLogout task = new TaskLogout(bmsListener, instance, Authkey);
		put(bmsListener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);

		return 0;
	}

	@Override
	public int taskCreateSchedule(String authenKey, String code, String name,
			int User_id, String Execute_date, String Price, int Hospital_id,
			String Address, String Require, int Level, int Case_id,
			String AddInfos, String TypeCost, int SchedUserRole_id,
			BMSListener listener) {
		TaskCreateSchedule taskCreateSchedule = new TaskCreateSchedule(
				listener, instance, authenKey, code, name, User_id,
				Execute_date, Price, Hospital_id, Address, Require, Level,
				Case_id, AddInfos, TypeCost, SchedUserRole_id);
		put(listener, taskCreateSchedule.getClass().getName(), PRIORITY_MEDIUM,
				taskCreateSchedule, true);
		return 0;
	}

	@Override
	public int taskUpdateAndroidSchedule(String Code, String Name,
			String Price, String Execute_date, String AuthKey, int Hospital_id,
			String Address, int Level, int Case_id, String AddInfos,
			int SchedUserRole_id, String TypeCost, String ID, String Require,
			int User_id, BMSListener listener) {
		TaskUpdateAndroidSchedule task = new TaskUpdateAndroidSchedule(
				listener, instance, Code, Name, Price, Execute_date, AuthKey,
				Hospital_id, Address, Level, Case_id, AddInfos,
				SchedUserRole_id, TypeCost, ID, Require, User_id);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskGetTypeUser(String authenKey, int Group_id, int pageLimit,
			int pageNumber, BMSListener listener) {
		
		TaskGetTypeUser taskGetTypeUser = new TaskGetTypeUser(listener,instance, authenKey, Group_id, pageLimit, pageNumber);
		put(listener, taskGetTypeUser.getClass().getName(), PRIORITY_MEDIUM,taskGetTypeUser, true);
		
		return 0;
	}

	@Override
	public int taskGetSchedule(String authenKey, BMSListener listener) {
		TaskGetSchedule taskGetSchedule = new TaskGetSchedule(listener,
				instance, authenKey);
		put(listener, taskGetSchedule.getClass().getName(), PRIORITY_MEDIUM,
				taskGetSchedule, true);
		return 0;
	}

	@Override
	public int taskGetHopital(String authenKey, BMSListener listener) {
		TaskGetHopital taskGetHopital = new TaskGetHopital(listener, instance,
				authenKey);
		put(listener, taskGetHopital.getClass().getName(), PRIORITY_MEDIUM,
				taskGetHopital, true);
		return 0;
	}

	@Override
	public int taskGetComment(String authenKey, String Schedule_id,
			String pageLimit, String pageNumber, BMSListener listener) {
		TaskGetComment taskGetComment = new TaskGetComment(listener, instance,
				authenKey, Schedule_id, pageLimit, pageNumber);
		put(listener, taskGetComment.getClass().getName(), PRIORITY_MEDIUM,
				taskGetComment, true);
		return 0;
	}

	@Override
	public int taskGetAndroidUserInfo(String authKey, BMSListener listener) {
		// TODO Auto-generated method stub
		TaskGetUserInfo task = new TaskGetUserInfo(listener, instance, authKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskEditUserInfo(EditUserModel editUserModel, String AuthKey,
			BMSListener listener) {
		// TODO Auto-generated method stub
		TaskEditUserInfo task = new TaskEditUserInfo(listener, instance,
				editUserModel, AuthKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskgetListSurgery(int sortby, int groupby, String ObjId,
			String AuthKey, int pageLimit, int pageNumber, BMSListener listener) {
		// TODO Auto-generated method stub
		TaskGetListSurgery task = new TaskGetListSurgery(listener, instance,
				sortby, groupby, ObjId, AuthKey, pageLimit, pageNumber);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskGetDetailSergery(String id, String AuthKey,
			BMSListener listener) {
		// TODO Auto-generated method stub
		TaskGetDetailSergery task = new TaskGetDetailSergery(listener,
				instance, id, AuthKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskAddTechnical(String Schedule_id, String User_id,
			String AuthKey, BMSListener listener) {
		TaskAddTechnical task = new TaskAddTechnical(listener, instance,
				Schedule_id, User_id, AuthKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskSentStatusSchedule(String ID, String Status, String AuthKey,
			BMSListener listener) {
		TaskSentStatusSchedule task = new TaskSentStatusSchedule(listener,
				instance, ID, Status, AuthKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskSendComment(String Schedule_id, String Content,
			String title, String Comment_date, String AuthKey,
			BMSListener listener) {
		TaskSendComment task = new TaskSendComment(listener, instance,
				Schedule_id, Content, title, Comment_date, AuthKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskGetListNotify(String AuthKey, int pageLimit, int pageNumber,
			BMSListener listener) {
		TaskGetListNotification task = new TaskGetListNotification(listener,
				instance, AuthKey, pageLimit, pageNumber);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskGetListCompany(String authenKey, BMSListener listener) {
		// TODO Auto-generated method stub
		TaskGetListCompany task = new TaskGetListCompany(listener, instance,
				authenKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskGetListMedicalRecord(String authenKey, BMSListener listener) {
		// TODO Auto-generated method stub
		TaskGetListMedicalRecord task = new TaskGetListMedicalRecord(listener,
				instance, authenKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskDeleteTechnical(String authenKey, String ID, String reason,
			BMSListener listener) {
		// TODO Auto-generated method stub
		TaskDeleteTechnical task = new TaskDeleteTechnical(listener, instance,
				ID, reason, authenKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskGetListMesseger(int Type, int pageLimit, int pageNumber,
			String Authkey, BMSListener listener) {
		// TODO Auto-generated method stub
		TaskGetListMesseger task = new TaskGetListMesseger(listener, instance,
				Type, pageLimit, pageNumber, Authkey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskSendMesseger(String UserId, String GroupsId, String Content,
			String Title, String AuthKey, BMSListener listener) {
		// TODO Auto-generated method stub
		TaskSendMesseger task = new TaskSendMesseger(listener, instance,
				UserId, Title, Content, AuthKey,GroupsId);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	

	@Override
	public int taskGetListCourses(int pageLimit, int pageNumber,
			BMSListener listener) {
		// TODO Auto-generated method stub
		TaskGetListCourses task = new TaskGetListCourses(listener, instance,
				pageLimit, pageNumber);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	public int taskPoseNotParticipate(String status_id, String iD,
			String reason, String userAuthKey, BMSListener listener) {
		TaskPoseNotParticipate task = new TaskPoseNotParticipate(listener,
				instance, status_id, iD, reason, userAuthKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;

	}

	@Override
	public int taskGetListPromotions(int pageLimit, int pageNumber,
			BMSListener listener) {
		// TODO Auto-generated method stub
		TaskGetListPromotions task = new TaskGetListPromotions(listener,
				instance, pageLimit, pageNumber);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskGetListProduct(int pageLimit, int pageNumber,
			BMSListener listener) {
		// TODO Auto-generated method stub
		TaskGetListProduct task = new TaskGetListProduct(listener, instance,
				pageLimit, pageNumber);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskGetListSeminar(int pageLimit, int pageNumber,
			BMSListener listener) {
		// TODO Auto-generated method stub
		TaskGetListSeminar task = new TaskGetListSeminar(listener, instance,
				pageLimit, pageNumber);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskGetAndroidNotifications(String AuthenKey,
			BMSListener listener) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int taskSendToAdmin(String Content, String title, String group_Id,
			String AuthKey, BMSListener listener) {
		// TODO Auto-generated method stub
		TaskSendToAdmin task = new TaskSendToAdmin(listener, instance, Content,
				title, group_Id, AuthKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskGetDetailMesseger(String Msg_id, String AuthKey,
			int pagelimit, int pagenumber, BMSListener listener) {
		// TODO Auto-generated method stub
		TaskGetDetailMesseger task = new TaskGetDetailMesseger(listener,
				instance, Msg_id, AuthKey, pagelimit, pagenumber);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);

		return 0;
	}

	@Override
	public int taskGetDetailBMSProduct(String ID, int TypeBMS,
			BMSListener listener) {
		// TODO Auto-generated method stub
		TaskGetProductBMS task = new TaskGetProductBMS(listener, instance, ID,
				TypeBMS);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);

		return 0;
	}

	@Override
	public int taskGetDetailReportFile(int type, String CompanyId,
			String CaseId, String DateStart, String DateEnd, String Authkey,
			BMSListener listener) {
		// TODO Auto-generated method stub
		TaskGetDetailReportFile task = new TaskGetDetailReportFile(listener,
				instance, type, CompanyId, CaseId, DateStart, DateEnd, Authkey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);

		return 0;
	}

	@Override
	public int taskGetDetailReportByType(int type, String ObjId, int SortBy,
			String DateStart, String DateEnd, String AuthKey,
			BMSListener listener) {
		// TODO Auto-generated method stub
		TaskGetDetailReportByType task = new TaskGetDetailReportByType(
				listener, instance, type, ObjId, SortBy, DateStart, DateEnd,
				AuthKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);

		return 0;
	}

	@Override
	public int taskGetSupport(String authKey, BMSListener listener) {
		// TODO Auto-generated method stub
		TaskGetSupport task = new TaskGetSupport(listener, instance, authKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskGetComDoctor(String authKey, String Date, String comID,
			BMSListener listener) {
		// TODO Auto-generated method stub
		TaskGetComDoctor task = new TaskGetComDoctor(listener, instance,
				authKey, Date, comID);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskSendUpdateComDoctor(ArrayList<InputDataModel> Data,
			String Date, String AuthKey, BMSListener listener) {
		// TODO Auto-generated method stubs
		TaskSendUpdateComDoctor task = new TaskSendUpdateComDoctor(listener,
				instance, Data, Date, AuthKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskGetListComHospital(String Date, String companyID,
			String AuthKey, BMSListener listener) {
		// TODO Auto-generated method stub
		TaskGetListComHospital task = new TaskGetListComHospital(listener,
				instance, Date, companyID, AuthKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskGetListMedicalRecordFollowHos(String Date,
			String Company_id, String Hospital_id, String AuthenKey,
			BMSListener listener) {
		// TODO Auto-generated method stub
		TaskGetListMedicalRecordHospital task = new TaskGetListMedicalRecordHospital(
				listener, instance, Date, Company_id, Hospital_id, AuthenKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskSendUpdateHosAndRecord(String Con_hos_id, String Revenue,
			ArrayList<HospitalRecordInputDataModel> DataRecord, String Date,
			String AuthKey, BMSListener listener) {
		// TODO Auto-generated method stub
		TaskSendUpdateHospitalRecord task = new TaskSendUpdateHospitalRecord(
				listener, instance, AuthKey, Con_hos_id, Revenue, DataRecord,
				Date);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskSetReadNotify(String id, String AuthKey, BMSListener listener) {
		// TODO Auto-generated method stub
		TaskSetReadNotify task = new TaskSetReadNotify(listener, instance, id,
				AuthKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);

		return 0;
	}

	@Override
	public int GetTimeNotification(BMSListener listener) {
		TaskGetTimeNotification task = new TaskGetTimeNotification(listener,
				instance);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskSetReadMessage(String id, String AuthKey,
			BMSListener listener) {
		// TODO Auto-generated method stub
		TaskSetReadMessage task = new TaskSetReadMessage(listener, instance,
				id, AuthKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);

		return 0;
	}

	@Override
	public int taskSendDeleteNotify(String listId, String AuthKey,
			BMSListener listener) {
		// TODO Auto-generated method stub
		TaskSendDeleteNotify task = new TaskSendDeleteNotify(listener,
				instance, listId, AuthKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;

	}

	@Override
	public int taskSendDeleteMessager(String listId, String AuthKey,
			BMSListener listener) {
		// TODO Auto-generated method stub
		TaskSendDeleteMessager task = new TaskSendDeleteMessager(listener,
				instance, listId, AuthKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);

		return 0;
	}

	@Override
	public int taskGetMessegertwoGroup(String ObjId, String AuthKey,
			int pageLimit, int pageNumber, BMSListener listener) {
		// TODO Auto-generated method stub
		TaskGetMessegerTwoGroups task = new TaskGetMessegerTwoGroups(listener, instance, ObjId, AuthKey, pageNumber, pageLimit);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskSendMsgtoGroups(String title, String Content,
			String groupId, String authKey, BMSListener listener) {
		// TODO Auto-generated method stub
		TaskSendMsgtoGroups task = new TaskSendMsgtoGroups(listener, instance, title, Content, groupId, authKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskGetChatHints(String AuthKey, BMSListener listener) {
		// TODO Auto-generated method stub
		TaskGetListHints task = new TaskGetListHints(listener, instance, AuthKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);
		return 0;
	}

	@Override
	public int taskDeleteMsg(String Id, String AuthKey, BMSListener listener) {
		// TODO Auto-generated method stub
		TaskDeleteMsg task = new TaskDeleteMsg(listener, instance, Id, AuthKey);
		put(listener, task.getClass().getName(), PRIORITY_MEDIUM, task, true);

		return 0;
	}



}
